home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1550 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.2 KB  |  46 lines

  1. Path: ix.netcom.com!netnews
  2. From: miker3@ix.netcom.com (Mike Rubenstein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: is this string a number?
  5. Date: Mon, 15 Jan 1996 01:32:06 GMT
  6. Organization: Netcom
  7. Message-ID: <30f9ac87.195826304@nntp.ix.netcom.com>
  8. References: <4dbogk$763@jupiter.planet.net>
  9. NNTP-Posting-Host: ix-dc13-06.ix.netcom.com
  10. X-NETCOM-Date: Sun Jan 14  5:32:02 PM PST 1996
  11. X-Newsreader: Forte Agent .99c/16.141
  12.  
  13. Chris Kemp <chrisk@paladn.com> wrote:
  14.  
  15. |>I am inputting a string from the keyboard from a user, and 
  16. |>intend to use the atol or strtol functions to convert the 
  17. |>string to a number.
  18. |>
  19. |>But first I must make sure the user provided a legitimate 
  20. |>number (not line 1234B).
  21. |>
  22. |>Is there a library function which will make this determination 
  23. |>for me, or must I make up one of my own with somthing like 
  24. |>strtok?
  25.  
  26. Use strtol() and supply a non-null second argument.  When strtol()
  27. returns, the pointer will point to the first character that was not
  28. converted.  Example:
  29.  
  30.     #include <stdlib.h>
  31.  
  32.     char *str;
  33.     char *nstr;
  34.     long l;
  35.  
  36.     /* ... */
  37.  
  38.     l = strtol(str, &nstr, 10);
  39.     if (nstr == str || *nstr !=  '\0')
  40.     {
  41.       /* str was empty or there is an illegal character */
  42.     }
  43.  
  44.  
  45. Michael M Rubenstein
  46.